#include <vector>
#include<limits>

class Solution
{
public:
    int minSubArrayLen(int target, std::vector<int>& nums)
    {
        int n = nums.size();
        if (target == 0) return 0;
        if (n == 0) return 0;
        int flag = 1;
        int minLen = std::numeric_limits<int>::max();
        int sum = 0;
        for (int r = 0,l=0; r < n; r++)
        {
            sum += nums[r];
            while (sum-nums[l] >= target)
            {
                sum -= nums[l++];
            }
        if (sum>=target)
        {
            flag = 0;
            minLen = std::min(minLen, r-l+1);
        }
        }
        
        return flag==0?minLen:0;
    }
};
